<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class question_class extends AWS_MODEL
{

	/**
	 * 得到具有相同回复的问题结合
	 * @param int $question_id
	 * 
	 * @return array
	 */
	public function get_question_same_answer($question_id, $answer_count, $limit = 20, $order_by = 'answer_count, question_id DESC')
	{
		$where = "question_id <> " . intval($question_id);
		
		if (! empty($answer_count))
		{
			$where .= " AND answer_count = " . intval($answer_count);
		}
		
		return $this->fetch_all('question', $where, $order_by, $limit);
	}

	public function get_topic_category_count($category_id)
	{
		return $this->count('question', 'category_id IN(' . implode(',', $this->model('system')->get_category_with_child_ids('question', $category_id)) . ')');
	}

	/**
	 * 
	 * 得到问题列表
	 * @param int $limit
	 */
	public function get_questions($limit = 10, $order_by = 'add_time DESC', $where = null)
	{
		return $this->fetch_all('question', $where, $order_by, $limit);
	}

	/**
	 * 
	 * 得到新问题列表
	 * @param int $limit
	 */
	public function get_new_questions_id($limit = 10, $order = 'add_time DESC', $category_id = null)
	{
		if ($category_id)
		{
			$where = 'category_id IN(' . implode(',', $this->model('system')->get_category_with_child_ids('question', $category_id)) . ')';
		}
		$question_list = $this->fetch_all('question', $where, $order, $limit);
		
		$question_id_list = array();
		
		if ($question_list)
		{
			foreach ($question_list as $question_info)
			{
				$question_id_list[] = $question_info['question_id'];
			}
		}
		return $question_id_list;
	}

	/**
	 *得到问题
	 */
	public function get_question($page = 1, $pre_page = 10, $sort = 'hot', $topic_id = 0, $uid = null, $category_id = null, $answer_count = null, $day = 30)
	{
		$topic_id = intval($topic_id);
		$uid = intval($uid);
		
		$user_id_list = array();
		
		$answer_info_list = array();
		$answer_list = array();
		$user_info_list = array();
		$user_list = array();
		$question_info_list = array();
		$question_list = array();
		
		$limit = calc_page_limit($page, $pre_page);
		
		if ($sort == 'hot')		
		{
			$question_info_list = $this->get_hot_question_v2(0, $category_id, $limit, $day);
			
			$question_ids = array();
			
			if ($question_info_list)
			{
				foreach ($question_info_list as $key => $data)
				{
					$question_ids[] = $data['question_id'];
				}
			}
		}
		else
		{
			switch ($sort)
			{
				default :
					$order_key = 'add_time';
				break;
			
				case 'new' :
					$order_key = 'update_time';
				break;
			}
			
			if ($topic_id)
			{
				
				$question_info_list = $this->model('question')->get_question_info($topic_id, $limit, 0, false, $order_key, $category_id, $answer_count);
				
				$question_ids = array();
				
				if ($question_info_list)
				{
					foreach ($question_info_list as $key => $data)
					{
						$question_ids[] = $data['question_id'];
					}
				}
			}
			else
			{
				if ($question_ids = $this->model('question')->get_new_questions_id($limit, $order_key . ' DESC', $category_id))
				{
					$question_info_list = $this->fetch_all('question', 'question_id IN(' . implode(',', array_unique($question_ids)) . ')', $order_key . ' DESC');
				}
			}
		}
		
		if (!is_array($question_ids))
		{
			return array();
		}
		
		foreach ($question_ids as $question_id)
		{
			$answers = $this->model('answer')->get_answer_list_by_question_id($question_id, 1);
			
			$answer_info_list[$question_id] = $answers[0];
		}
		
		if ($answer_info_list)
		{
			foreach ($answer_info_list as $answer_info)
			{
				if (! in_array($answer_info['uid'], $user_id_list) AND $answer_info['uid'])
				{
					$user_id_list[] = $answer_info['uid'];
				}
			}
		}
		
		if ($question_info_list)
		{
			foreach($question_info_list as $keyx => $question_info)
			{
				if (! in_array($question_info['published_uid'], $user_id_list))
				{
					$user_id_list[] = $question_info['published_uid'];
				}
			}
		}
		
		$categorys = $this->model('system')->get_category_list('question');
		
		if ($user_info_list = $this->model('account')->get_users_by_uids($user_id_list, true))
		{			
			foreach ($user_info_list as $user_info)
			{				
				$tmp = array();
				$tmp['uid'] = $user_info['uid'];
				$tmp['user_name'] = $user_info['user_name'];
				$tmp['signature'] = $user_info['signature'];
				$tmp['avatar_file'] = $user_info['avatar_file'];
				
				$user_list[$user_info['uid']] = $tmp;
			}
		}
		
		//格式话回复格式
		if($answer_info_list)
		{
			foreach ($answer_info_list as $answer_info)
			{
				if (! $answer_info)
				{
					continue;
				}
				
				$tmp = array();
				$tmp['uid'] = $answer_info['uid'];
				$tmp['answer_id'] = $answer_info['answer_id'];
				$tmp['question_id'] = $answer_info['question_id'];
				$tmp['user_name'] = $user_list[$answer_info['uid']]['user_name'];
				$tmp["agree_count"] = $answer_info["agree_count"] * 1;
				$tmp['modify_time'] = $answer_info['modify_time'] > 0 ? ($answer_info['modify_time'] * 1) : ($answer_info['add_time'] * 1);
				$tmp['attachs'] = $this->model("answer")->get_answer_attach($answer_info['answer_id'], 'min');
				
				if (isset($answer_info['answer_content']))
				{
					$answer_list_content = FORMAT::format_content($answer_info['answer_content']);
					
					$tmp['answer_content'] = $answer_list_content['content_content'];
					$tmp['answer_title'] = $answer_list_content['content_title'];
				}
				
				$answer_list[$answer_info['question_id']] = $tmp;
			}
		}
		
		if ($question_info_list)
		{
			//格式话问题格式
			foreach ($question_info_list as $question_info)
			{
				$tmp = array();
				$top_tmp = array();
				
				$tmp['title'] = $question_info['question_content'];
				
				$tmp['question_detail'] = $question_info['question_detail'];
				
				$tmp['category_id'] = $question_info['category_id'];
				
				// 获取附件
				$tmp['attachs'] = $this->model('question')->get_question_attach($question_info['question_id'], 'min');
				
				$tmp['category_info'] = $this->model('system')->get_category_info($question_info['category_id']); 
			
				/*if ($uid)
				{
					$tmp['focus'] = $this->model('question')->has_focus_question($question_info['question_id'], $uid);
				}*/
			
				$tmp['topics'] = $this->model('question_topic')->get_question_topic_by_question_id($question_info['question_id']);
				
				$tmp['vote_count'] = $answer_info['agree_count'];
				
				$tmp['answer'] = $answer_list[$question_info['question_id']];
				
				$tmp['question_id'] = $question_info['question_id'];
				
				$tmp['answer_count'] = $question_info['answer_count'];
				$tmp['focus_count'] = $question_info['focus_count'];
				$tmp['view_count'] = $question_info['view_count'];
				
				$tmp['user_info'] = $user_list[$question_info['published_uid']];
				
				$tmp['add_time'] = $question_info['add_time'];
				
				$tmp['update_time'] = $question_info['update_time'];
				
				$tmp['category'] = $categorys[$question_info['category_id']];
				
				$question_list[] = $tmp;
			}
		}

		return $question_list;
	}
	
	public function get_discuss_list($page, $pre_page, $sort_type, $topic_id, $user_id, $category, $answer_count, $day)
	{
		$cache_key = ZCACHE::format_key("discuss_". $page . "_" . $sort_type . "_" . $topic_id."_".$user_id . "_".$category."_".$answer_count."_".$day);
		
		$question_list = ZCACHE::get($cache_key);
		
		if ($question_list === false)
		{
			$question_list = $this->get_question($page, $pre_page, $sort_type, $topic_id, $user_id, $category, $answer_count, $day);
			
			ZCACHE::set($cache_key, $question_list, null, get_setting('cache_level_high'));
		}
		
		return $question_list;
	}
	
	public function get_hot_question_v2($add_time = 0, $category_id = 0, $limit = '0, 10', $day = 30)
	{
		$limit_tmp = preg_split("/,/", $limit);
		
		$offset = $limit_tmp[0] * 1;
		$length = $limit_tmp[1] * 1;
		
		$question_ids[] = 0;
		
		$day = intval($day);
		
		if ($day < 1)
		{
			$day = 30;
		}
		
		$hot_question_period = get_setting('hot_question_period') * 1;
		
		if ($hot_question_period < 1)
		{
			$hot_question_period = 30;
		}
		
		
		if ($add_time == 0)
		{
			if($day==1)//当前的从当前时间0点算起
			{
				$add_time=mktime(0, 0, 0, date("m"), date("d"), date("Y"));
			}
			else
			{
				$add_time = mktime() - 60 * 60 * 24 * $day; //只处理30天内的(可调)
			}
		}
		
		if ($category_id)
		{
			$question_all = $this->fetch_all('question', "add_time > " . $add_time . " AND category_id IN(" . implode(',', $this->model('system')->get_category_with_child_ids('question', $category_id)) . ')');
		
		}
		else
		{
			$question_all = $this->fetch_all('question', "add_time > " . $add_time);
		}
		
		if ($question_all)
		{
			foreach ($question_all as $key => $val)
			{
				$question_ids[] = $val['question_id'];
			
			}
		}
		$scores_tmp_up = 0;
		$scores_tmp_down = 0;
		$scores = 0;
		
		//计算分数
		foreach ($question_all as $key => $val)
		{

			$scores_tmp_up = @(log10($val['view_count']) + log($val['focus_count'], 3) + pow($val['agree_count'],2) / ($val['agree_count'] + $val['against_count']) + $val['answer_count']);			
			$scores_tmp_down = @(time() - $add_time);
			$scores_tmp_down = @(round($scores_tmp_down / 3600, 1));
					
			$scores = $scores_tmp_up / $scores_tmp_down;			
			$question_all[$key]['scores'] = exp($scores);
		}
		
		if (is_array($question_all))
		{
			$question_all = aasort($question_all, array(
				'-scores'
			));
			
			if ($question_all)
			{
				return array_slice($question_all, $offset, $length);
			}
			else
			{
				return array();
			}
		}
		
		return false;
	}

	
	
	public function calculate_question_score_hot($add_time = 0, $category_id = 0, $limit = '0, 10', $day = 30)
	{
	

	}	
	
	/**
	 * 
	 * 根据问题ID,得到关注用户列表
	 */
	public function get_focus_uid_by_question_id($question_id)
	{
		$sql = "SELECT uid, question_content FROM " . $this->get_table('question_focus') . " AS qf, " . $this->get_table('question') . " AS q WHERE qf.question_id = " . intval($question_id) . " AND qf.question_id = q.question_id ";
		
		return $this->query_all($sql);
	}

	/**
	 * 
	 * 根据用户ID，得到用户问过的问题列表
	 * @param boolean $count
	 * @param int $uid
	 * @param int $limit
	 * 
	 * @return array
	 */
	public function get_question_list_by_uid($count = false, $uid = 0, $limit = 20)
	{
		$uid = intval($uid);
		
		if ($uid == 0)
		{
			return false;
		}
		if ($count)
		{
			return $this->count('question', "published_uid = " . $uid);
		}
		else
		{
			return $this->fetch_all('question', "published_uid = " . $uid, 'question_id DESC', $limit);
		}
	
	}

	/**
	 * 得到用户发布的列表
	 * @param  $count
	 * @param  $uids array
	 * @param  $limit
	 */
	public function get_question_list_by_uids($count = false, $uids_array, $limit = 20)
	{
		if (! is_array($uids_array) || empty($uids_array))
		{
			return false;
		}
		
		$uids = implode(',', $uids_array);
		
		if ($count)
		{
			return $this->count('question', "published_uid iN(" . $uids . ")", 'question_id DESC');
		}
		else
		{
			return $this->fetch_all('question', "published_uid IN(" . $uids . ")", 'question_id DESC', $limit);
		}
	}

	/**
	 * 
	 * 得到问题列表简单信息
	 * @param int    $topic_id 话题ID 如为0则选择全部问题 
	 * @param string $limit 条数限制
	 * @param int    $uid
	 * 
	 * @return array
	 */
	public function get_question_info($topic_id = 0, $limit = "", $uid = 0, $return_count = false, $order_key = null, $category_id = null, $answer_count = null)
	{
		$where = array();
		
		$topic_id = intval($topic_id);
		
		if (! $order_key)
		{
			$order_key = 'add_time';
		}
		else
		{
			$order_key = $this->quote($order_key);
		}
		
		if ($topic_id == 0)
		{
			$uninterested_question = $this->get_question_uninterested($uid);
			
			if (! empty($uninterested_question))
			{
				$where[] = "q.question_id NOT IN(" . implode(",", $uninterested_question) . ")";
			}
			
			if (! empty($category_id))
			{
				$where[] = 'q.category_id IN(' . implode(',', $this->model('system')->get_category_with_child_ids('question', $category_id)) . ')';
			}
			
			$sql = "FROM " . $this->get_table('question') . " AS q LEFT JOIN " . $this->get_table('users') . " AS u ON q.published_uid = u.uid";
			
			if ($where)
			{
				$sql .= ' WHERE ' . implode(' AND ', $where);
			}
			
			if ($return_count)
			{
				$sql_pre = "SELECT COUNT(*) AS count ";
				
				$data = $this->query_row($sql_pre . $sql);
				
				return $data['count'];
			}
			else
			{
				$sql_pre = "SELECT *, q.answer_count as answer_count, u.user_name, u.avatar_file ";
				
				$sql = $sql_pre . $sql;
			}
			
			$sql .= " ORDER BY " . $order_key . " DESC ";
		}
		else
		{
			$sql = "SELECT q.*,tn.*, q.answer_count as answer_count FROM " . $this->get_table('question') . " AS q ";
			$sql .= "LEFT JOIN " . $this->get_table('topic_question') . " AS ql ON q.question_id = ql.question_id ";
			$sql .= "LEFT JOIN " . $this->get_table('topic') . " AS tn ON ql.topic_id = tn.topic_id ";
			$sql .= "WHERE tn.topic_id = " . intval($topic_id);
			
			if ($answer_count !== null)
			{
				$sql .= " AND  q.answer_count = " . intval($answer_count);
			}
			
			if (! empty($category_id))
			{
				$sql .= ' AND q.category_id IN(' . implode(',', $this->model('system')->get_category_with_child_ids('question', $category_id)) . ')';
			}
			
			$sql .= " ORDER BY q." . $order_key . " DESC";
			
			if ($return_count)
			{
				$sql = "SELECT COUNT(*) AS count FROM " . $this->get_table('question') . " AS q ";
				$sql .= "LEFT JOIN " . $this->get_table('topic_question') . " AS ql ON q.question_id = ql.question_id ";
				$sql .= "LEFT JOIN " . $this->get_table('topic') . " AS tn ON ql.topic_id = tn.topic_id ";
				$sql .= "WHERE tn.topic_id = " . intval($topic_id);
				if ($answer_count !== null)
				{
					$sql .= " AND q.answer_count = " . intval($answer_count);
				}
				
				if (! empty($category_id))
				{
					$sql .= ' AND q.category_id IN(' . implode(',', $this->model('system')->get_category_with_child_ids('question', $category_id)) . ')';
				}
				
				$data = $this->query_row($sql);
				
				return $data['count'];
			}
		}
		
		return $this->query_all($sql, $limit);
	}

	/**
	 * 获取问题列表
	 * Enter description here ...
	 * @param $count
	 * @param $keyword
	 * @param $limit
	 */
	public function get_question_list($count = false, $keyword = '', $limit = 10, $orderby = 'add_time DESC', $where = null)
	{
		$_where = array();
		
		if ($where)
		{
			$_where[] = $where;
		}
		
		if (! empty($keyword))
		{
			$_where[] = "(question_content LIKE '%{$keyword}%' OR question_detail LIKE '%{$keyword}%')";
		}
		
		if ($count)
		{
			return $this->count('question', implode(' AND ', $_where));
		}
		else
		{
			return $this->fetch_all('question', implode(' AND ', $_where), $orderby, $limit);
		}
	}

	/**
	 * 
	 * 得到单条问题信息
	 * 
	 * @return array
	 */
	public function get_question_info_by_id($question_id)
	{
		if (!$question_id)
		{
			return false;
		}
		
		static $questions;
		
		if ($questions[$question_id])
		{
			return $questions[$question_id];
		}
		
		$questions[$question_id] = $this->fetch_row('question', "question_id = " . intval($question_id));
		
		return $questions[$question_id];
	}
	
	
	/**
	 * 获取问题关键字列表
	 * @param  $question_id
	 * @param  $count
	 */
	public function get_question_keyword_list($question_id,$count=false)
	{
		$question_id=intval($question_id);
	
	
		if ($count)
		{
			return $this->count('question_keyword', 'question_id='.$question_id);
		}
		else
		{
			return $this->fetch_all('question_keyword', 'question_id='.$question_id);
		}
	}
	
	public function get_question_list_by_keywords($keyword_array=array())
	{
	
		if(!is_array($keyword_array))
		{
			return array();
		}
		
		if(count($keyword_array)<1)
		{
			return array();
		}
		
		//$sql=' SELECT * FROM '.get_table('question').' WHERE question_id IN 
		//(SELECT question_id FROM '.get_table('question_keyword').' WHERE keyword IN('.implode(',', $keyword_array).'))';

		$sql='SELECT question_id,COUNT(*) AS COUNT  FROM '.get_table('question_keyword').' WHERE keyword IN('.implode(',', $keyword_array).') GROUP BY question_id ORDER BY COUNT DESC';
	
		$rs=$this->query_all($sql);
		
		if($rs)
		{
			
			foreach($rs as $key=>$val)
			{
				$questions[]=$val['question_id'];
				$questions_keyword_count[$val['question_id']]=$val['COUNT'];				
			}			
			$sql=' SELECT * FROM '.get_table('question').' WHERE question_id IN ('.implode(',', $questions).')';
			
			$rs=$this->query_all($sql);
			
			foreach($rs as $key=>$val)
			{
				$rs[$key]['keyword_count']=$questions_keyword_count[$val['question_id']];				
			}
			
			
			
			$rs = aasort($rs, array(
					'-keyword_count'
			));
				
			
			return $rs;
			
		}
		else			
		{
			return array();
		}
		
	}	
	

	/**
	 * 
	 * 增加问题浏览次数记录
	 * @param int $question_id
	 * 
	 * @return boolean true|false
	 */
	public function increament_question_visist_count($question_id)
	{		
		return $this->query("UPDATE " . $this->get_table('question') . " SET view_count = view_count + 1 WHERE question_id = " . intval($question_id));
	}

	/**
	 * 
	 * 增加问题内容
	 * @param string $question_content //问题内容
	 * @param string $question_detail  //问题说明
	 * @param int $add_time //添加时间
	 * @param int $modify_time //最后修改时间
	 * @param int $published_uid //发布用户UID
	 * @param int $modify_uid //最后修改用户UID
	 * @param int $answer_count //回复计数
	 * @param int $view_count //浏览次数
	 * @param int $focus_count //关注数
	 * @param int $last_action //最后操作的的类型 1回复 2关注 3赞同 4反对
	 * 
	 * @return boolean true|false
	 */
	public function save_question($question_content, $question_detail, $add_time = 0, $modify_time = 0, $published_uid = 0, $modify_uid = 0, $answer_count = 0, $view_count = 0, $focus_count = 0, $last_action = 0, $last_uid = 0, $point = 0, $action_log = true)
	{
		$published_uid = (intval($published_uid) == 0) ? USER::get_client_uid() : $published_uid;
		$modify_uid = (intval($modify_uid) == 0) ? USER::get_client_uid() : $modify_uid;
		$last_uid = (intval($last_uid) == 0) ? USER::get_client_uid() : $last_uid;
		
		$data = array(
			"question_content" => htmlspecialchars($question_content), 
			"question_detail" => htmlspecialchars($question_detail), 
			"add_time" => (intval($add_time) == 0) ? time() : $add_time, 
			"modify_time" => (intval($modify_time) == 0) ? time() : $modify_time, 
			"update_time" => (intval($modify_time) == 0) ? time() : $modify_time, 
			"published_uid" => $published_uid, 
			"modify_uid" => $modify_uid, 
			"answer_count" => intval($answer_count), 
			"view_count" => intval($view_count), 
			"focus_count" => intval($focus_count), 
			"last_action" => ACTION_LOG::ADD_QUESTION, 
			"last_uid" => $last_uid
		);
		
		$question_id = $this->insert('question', $data);
		
		//更新个人问题数量
		if ($question_id)
		{
			$this->update_user_question_count($published_uid);
		}
		
		if ($action_log)
		{
			//记录日志
			ACTION_LOG::save_action($published_uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::ADD_QUESTION, htmlspecialchars($question_content), htmlspecialchars($question_detail));
			
			//自动关注该问题
			$this->add_focus_question($question_id, false);
		}
		
		return $question_id;
	}

	function update_question_category($question_id, $category_id)
	{
		if (! $question_id or ! $category_id)
		{
			return false;
		}
		
		return $this->update('question', array(
			'category_id' => $category_id
		), 'question_id = ' . intval($question_id));
	}

	/**
	 * 更新用户问题计数器
	 */
	function update_user_question_count($uid)
	{
		if (empty($uid))
		{
			$uid = USER::get_client_uid();
		}
		
		$user_count = $this->get_question_list_by_uid(true, $uid);
		
		return $this->update('users', array(
			'question_count' => $user_count
		), 'uid = ' . intval($uid));
	}

	/**
	 * 
	 * 更改问题最后修改状态
	 * @param int $question_id
	 * @param int $modify_uid
	 * @param int $state
	 * 
	 * @return boolean true|false
	 */
	public function update_question_state($question_id, $modify_uid, $state, $last_uid = 0)
	{
		$question_id = intval($question_id);
		$modify_uid = intval($modify_uid);
		$last_uid = (intval($last_uid) == 0) ? USER::get_client_uid() : $last_uid;
		
		if ($question_id == 0 && $modify_uid == 0)
		{
			return false;
		}
		
		$sate_array = array(
			ACTION_LOG::ADD_QUESTION, 
			ACTION_LOG::ADD_REQUESTION_FOCUS, 
			ACTION_LOG::ANSWER_QUESTION, 
			ACTION_LOG::ADD_TOPIC, 
			ACTION_LOG::DELETE_REQUESTION, 
			ACTION_LOG::DELETE_REQUESTION_FOCUS, 
			ACTION_LOG::DELETE_ANSWER, 
			ACTION_LOG::DELETE_TOPIC, 
			ACTION_LOG::MOD_QUESTION_DESCRI, 
			ACTION_LOG::MOD_QUESTON_TITLE
		);
		
		if (! in_array($state, $sate_array))
		{
			return false;
		}
		
		$data = array(
			'last_action' => $state, 
			'last_uid' => $last_uid, 
			'modify_uid' => $modify_uid
		);
		
		return $this->update('question', $data, "question_id = " . $question_id);
	}

	/**
	 * 
	 * 修改问题内容
	 * @param array $data 修改问题内容
	 * @param int $question_id 问题ID
	 * @param int $modfiy_type 0 标题 1 描述
	 * 
	 * @return boolean true|false
	 */
	public function update_question($question_id, $question_content = "", $question_detail = "")
	{
		$question_id = intval($question_id);
		
		if ($question_id == 0)
		{
			return false;
		}
		
		$quesion_info = $this->get_question_info_by_id($question_id);
		$question_content = htmlspecialchars($question_content);
		$question_detail = htmlspecialchars($question_detail);
		
		$uid = USER::get_client_uid();
		
		$update_arr = array();
		$update_arr['modify_uid'] = $uid;
		$update_arr['modify_time'] = time();
		
		if (! empty($question_content))
		{
			$update_arr['question_content'] = $question_content;
		}
		
		if (! empty($question_detail))
		{
			$update_arr['question_detail'] = $question_detail;
		}
		
		$this->update('question', $update_arr, "question_id = " . $question_id);
		
		//自动关注该问题
		$this->add_focus_question($question_id, false);
		
		if ($quesion_info['question_detail'] != $question_detail)
		{
			$this->update_question_state($question_id, $uid, ACTION_LOG::MOD_QUESTION_DESCRI);
			ACTION_LOG::save_action($uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::MOD_QUESTION_DESCRI, htmlspecialchars($question_detail), $quesion_info['question_detail']);
		}
		
		//记录日志
		if ($quesion_info['question_content'] != $question_content)
		{
			$this->update_question_state($question_id, $uid, ACTION_LOG::MOD_QUESTON_TITLE);
			ACTION_LOG::save_action($uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::MOD_QUESTON_TITLE, htmlspecialchars($question_content), $quesion_info['question_content']);
		}
		
		return true;
	}

	/**
	 * 物理删除问题以及与问题相关的内容
	 * @param $question_id
	 */
	public function remove_question($question_id)
	{
		$question_id = intval($question_id);
		
		if (! $question_id)
		{
			return false;
		}
		
		$question_info = $this->get_question_info_by_id($question_id);
		
		if (empty($question_info))
		{
			return false;
		}
		
		$this->model('answer')->remove_answers_by_question_id($question_id); //删除关联的回复内容
		
		//删除评论
		$this->delete('question_comments',  "question_id = " . intval($question_id));

		$this->model("associate_index")->del_associate_index($question_info['published_uid'], $question_id, 3); //更新首页索引表
		
		$focus_users = $this->get_focus_users_by_question($question_id);
		
		if ($focus_users)
		{
			foreach ($focus_users as $key => $user)
			{
				$this->model("associate_index")->del_associate_index($user['uid'], $question_id, 1); //更新首页索引表
			}
			
			$this->delete('question_focus', "question_id = " . $question_id);
		}
		
		$this->delete('topic_question', "question_id = " . $question_id); //删除话题关联
		
		$this->delete('question_invite', "question_id = " . $question_id);	//删除邀请记录

		$this->delete('question_uninterested', "question_id = " . $question_id); //删除不感兴趣的

		$this->delete('user_action_history', "associate_type = 1 AND associate_id = " . $question_id); //删除动作
		

		//删除附件
		$attachs = $this->get_question_attach($question_id);
		
		if ($attachs)
		{
			foreach ($attachs as $key => $val)
			{
				$this->model('publish')->remove_question_attach($val['id'], $val['access_key']);
			}
		}
		
		$this->model('notify')->delete_notify('model_type = 1 AND source_id = ' . $question_id);	//删除相关的通知
		
		return $this->delete('question', "question_id = " . $question_id); //删除问题
	}

	public function remove_question_by_ids($question_ids)
	{
		if (empty($question_ids))
		{
			return false;
		}
		
		if ($question_ids = array_unique($question_ids))
		{
			foreach ($question_ids as $key => $val)
			{
				$this->remove_question($val);
			}
		}
		return true;
	}

	/**
	 * 
	 * 增加问题关注
	 * @param int $question_id
	 * @param boolean $auto_delete 以关注问题是否删除
	 * 
	 * @return boolean true|false
	 */
	public function add_focus_question($question_id, $auto_delete = true)
	{
		$question_id = intval($question_id);
		
		$uid = intval(USER::get_client_uid());
		
		if ($question_id == 0 || $uid == 0)
		{
			return false;
		}
		
		if (! $this->has_focus_question($question_id, $uid))
		{
			$data = array(
				"question_id" => $question_id, 
				"uid" => $uid, 
				"add_time" => time()
			);
			
			$retval = $this->insert('question_focus', $data);
			
			//增加问题关注数量
			if ($retval)
			{
				$this->update_focus_count($question_id);
			}
			
			//更改最后状态
			$this->update_question_state($question_id, $uid, ACTION_LOG::ADD_REQUESTION_FOCUS);
			//记录日志
			ACTION_LOG::save_action($uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::ADD_REQUESTION_FOCUS);
			
			//添加索引
			$this->model("associate_index")->add_associate_index($uid, $question_id, 1);
			
			return "add";
		}
		else
		{
			if ($auto_delete)
			{
				$retval = $this->delete_focus_question($question_id);
				//减少问题关注数量
				if ($retval)
				{
					$this->update_focus_count($question_id);
				}
			}
			
			//删除索引
			$this->model("associate_index")->del_associate_index($uid, $question_id, 1);
			
			return "remove";
		}
	}

	/**
	 * 
	 * 取消问题关注
	 * @param int $question_id
	 * 
	 * @return boolean true|false
	 */
	public function delete_focus_question($question_id)
	{
		$uid = USER::get_client_uid();
		
		if ($question_id == 0 || $uid == 0)
		{
			return false;
		}
		
		//更改最后状态
		$this->update_question_state($question_id, $uid, ACTION_LOG::DELETE_REQUESTION_FOCUS);
		
		//记录日志
		ACTION_LOG::save_action($uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::DELETE_REQUESTION_FOCUS);
		
		return $this->delete('question_focus', "question_id = " . $question_id . " AND uid = " . $uid);
	}

	/**
	 * 
	 * 根据用户，批量判断是否关注话题
	 * @param int $question_id
	 * @param int $uid
	 * 
	 * @return boolean true|false
	 */
	public function get_focus_question_by_uid(array $question_ids, $uid)
	{
		$data = array();
		
		$uid = intval($uid);
		
		if ($uid == 0)
		{
			return false;
		}
		
		$where[] = "uid = " . $uid;
		
		if (! empty($question_ids))
		{
			$ids = implode(",", $question_ids);
			
			$where[] = "question_id IN(" . $ids . ")";
		}
		
		if ($recordset_list = $this->fetch_all('question_focus', implode(' AND ', $where)))
		{
			foreach ($recordset_list as $record)
			{
				$data[] = $record['question_id'];
			}
		}
		return $data;
	}

	/**
	 * 根据用户,获取批量用户关注的问题.
	 * 
	 * @param bool $count;
	 * @param int  $uid
	 * @param string $limit
	 * 
	 * @return array $row
	 */
	public function get_focus_question_list_by_uid($count = false, $uid, $limit = '10')
	{
		$uid = intval($uid);
		
		if ($uid == 0)
		{
			return false;
		}
		
		if ($count) //查询数量的话返回count
		{
			return $this->count('question_focus', 'uid = ' . $uid);
		}
		
		//取出关注ID,不要用关联表查询,提高查询效率
		$rs_ids = $this->fetch_all('question_focus', "uid = " . $uid, "question_id DESC", $limit);
		
		if (empty($rs_ids))
		{
			return false;
		}
		
		foreach ($rs_ids as $key => $val)
		{
			$rs_ids_array[] = $val["question_id"];
		}
		
		$ids = implode(",", $rs_ids_array);
		
		//获取问题列表
		return $this->fetch_all('question', "question_id IN(" . $ids . ")", 'question_id DESC');
	}

	/**
	 * 根据用户ID,得到所有最后操作是该用户的所有问题
	 *  
	 * @param  $count
	 * @param  $uid
	 * @param  $limit
	 */
	public function get_question_list_for_last_uid($count = false, $uid, $limit = '10')
	{
		$uid = intval($uid);
		
		if ($uid == 0)
		{
			return false;
		}
		
		return $this->fetch_all('question', "last_uid = " . $uid, "question_id DESC");
	}

	/**
	 * 获取特定动作操作的问题列表ID集合(用户数组集)
	 *  
	 * @param  $count
	 * @param  $uids
	 * @param  $actions
	 * @param  $limit
	 */
	public function get_question_list_for_actions($count = false, $uids, $actions, $limit = '10')
	{
		if (! is_array($uids))
		{
			return false;
		}
		
		if (empty($uids))
		{
			return false;
		}
		
		$actions = trim($actions);
		
		if (empty($actions))
		{
			return false;
		}
		
		$where = " uid IN(" . implode(",", $uids) . ") AND associate_type = 1 AND associate_action IN(" . $actions . ") ";
		
		$question_list = ACTION_LOG::get_actions_distint_by_where($where, $limit);
		
		if (! $question_list)
		{
			return array();
		}
		
		foreach ($question_list as $key => $val)
		{
			$questions_array[] = $val["associate_id"];
		}
		
		$questions_array_str = implode(",", $questions_array);
		
		if ($count)
		{
			return $this->count('question', "question_id IN(" . $questions_array_str . ")");
		}
		else
		{
			return $this->fetch_all('question', "question_id IN(" . $questions_array_str . ")", "modify_time DESC", $limit);
		}
	}

	/**
	 * 根据用户,获取批量用户关注的问题.
	 * 
	 * @param bool $count;
	 * @param int  $uid
	 * @param string $limit
	 * 
	 * @return array $row
	 */
	public function get_focus_question_list_by_uids($count = false, $uid_array, $limit = '10')
	{
		//判断
		if (! is_array($uid_array))
		{
			return false;
		}
		
		if ($count) //查询数量的话返回count
		{
			return $this->count('question_focus', "uid IN( " . implode(",", $uid_array) . ")");
		}
		
		//取出关注ID,不要用关联表查询,提高查询效率
		$rs_ids = $this->fetch_all('question_focus', "uid IN( " . implode(",", $uid_array) . ")", '', $limit);
		
		if (empty($rs_ids))
		{
			return false;
		}
		
		$rs_ids_array = array();
		
		foreach ($rs_ids as $key => $val)
		{
			$rs_ids_array[] = $val["question_id"];
		}
		
		//获取问题列表
		return $this->fetch_all('question', "question_id IN(" . implode(",", $rs_ids_array) . ")", "question_id DESC");
	}

	/**
	 * 
	 * 判断是否已经关注问题
	 * @param int $question_id
	 * @param int $uid
	 * 
	 * @return boolean true|false
	 */
	public function has_focus_question($question_id, $uid)
	{
		if (intval($uid) <= 0)
		{
			return false;
		}
		
		if (intval($question_id) <= 0)
		{
			return false;
		}
		
		$retval = $this->fetch_row('question_focus', "question_id = " . intval($question_id) . " AND uid = " . intval($uid));
		
		if ($retval)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	function get_focus_users_by_question($question_id)
	{
		$uids = $this->query_all("SELECT DISTINCT uid FROM " . $this->get_table('question_focus') . " WHERE question_id = " . intval($question_id));
		
		$users_list = array();
		
		if ($uids)
		{
			foreach ($uids as $key => $user)
			{
				$user_info = $this->model('account')->get_users_by_uid($user['uid']);
				
				if ($user_info['uid'])
				{
					$users_list[$user_info['uid']] = array(
						'uid' => $user_info['uid'],
						'avatar_file' => $user_info['avatar_file'],
						'user_name' => $user_info['user_name']
					);
				}
			}
		}
		
		return $users_list;
	}

	public function get_user_focus($uid, $limit = 10)
	{
		$get_my_focus = $this->query_all('question_focus', "uid = " . (int)$uid . " AND question_id > 0");
		
		$my_focus = array();
		
		if ($get_my_focus)
		{
			foreach ($get_my_focus as $key => $val)
			{
				$my_focus[] = $val['question_id'];
			}
		}
		
		return $this->fetch_all('question', "question_id IN(" . implode(',', $my_focus) . ")", "add_time DESC", $limit);
	}

	public function get_user_publish($uid, $limit = 10)
	{
		return $this->fetch_all('question', "published_uid = " . intval($uid), "add_time DESC", $limit);
	}
	
	// 我关注的人关注的问题的话题下的问题
	public function get_user_recommend_v2($uid, $limit = 10)
	{
		$get_my_focus = $this->fetch_all('question_focus', "uid = " . (int)$uid . " AND question_id > 0");
		
		$my_focus = array(
			0
		);
		
		if ($get_my_focus)
		{
			foreach ($get_my_focus as $key => $val)
			{
				$my_focus[] = $val['question_id'];
			}
		}
		
		if ($friends = $this->model('follow')->get_user_friends($uid, false))
		{
			foreach ($friends as $key => $val)
			{
				$follow_uids[] = $val['friend_uid'];
			}
		}
		
		if (! $follow_uids)
		{
			return $this->get_questions($limit, "add_time DESC", "question_id NOT IN (" . implode(',', $my_focus) . ")");
		}
		
		$question_focus = $this->query_all("SELECT DISTINCT question_id FROM " . $this->get_table('question_focus') . " WHERE uid IN(" . implode($follow_uids, ',') . ") AND question_id NOT IN (" . implode($my_focus, ',') . ") ORDER BY add_time DESC", $limit);
		
		if ($question_focus)
		{
			foreach ($question_focus as $key => $val)
			{
				$questions_ids[] = $val['question_id'];
			}
		}
		if (! $questions_ids)
		{
			return $this->get_questions($limit, "add_time DESC", "question_id NOT IN (" . implode($my_focus, ',') . ")");
		}
		
		$topics = $this->query_all("SELECT DISTINCT topic_id FROM " . $this->get_table('topic_question') . " WHERE question_id IN(" . implode($questions_ids, ',') . ") AND question_id NOT IN (" . implode($my_focus, ',') . ") AND uid != " . (int)$uid . " ORDER BY topic_question_id DESC", $limit);
		
		if ($topics)
		{
			foreach ($topics as $key => $val)
			{
				$topics_ids[] = $val['topic_id'];
			}
		}
		
		if (! $topics_ids)
		{
			return $this->get_questions($limit, "add_time DESC", "question_id NOT IN (" . implode(',', $my_focus) . ")");
		}
		
		unset($questions_ids);
		
		$questions_ids_result = $this->query_all("SELECT DISTINCT question_id FROM " . $this->get_table('topic_question') . " WHERE topic_id IN(" . implode($topics_ids, ',') . ") AND question_id > 0 AND question_id NOT IN (" . implode($my_focus, ',') . ") AND uid != " . (int)$uid . " ORDER BY topic_question_id DESC", $limit);
		
		if ($questions_ids_result)
		{
			foreach ($questions_ids_result as $key => $val)
			{
				$questions_ids[] = $val['question_id'];
			}
		}
		
		if (! $questions_ids)
		{
			return $this->get_questions($limit, "add_time DESC", "question_id NOT IN (" . implode(',', $my_focus) . ")");
		}
		
		$questions = $this->fetch_all('question', "question_id IN(" . implode(',', $questions_ids) . ") AND published_uid != " . (int)$uid . " AND question_id NOT IN (" . implode(',', $my_focus) . ")", 'add_time DESC', $limit);
		
		if (sizeof($questions) < $limit)
		{
			$questions_new = $this->get_questions(($limit - sizeof($questions)), "question_id DESC", "question_id NOT IN(" . implode($questions_ids, ',') . ") AND question_id NOT IN (" . implode(',', $my_focus) . ")");
			
			if ($questions_new)
			{
				foreach ($questions_new as $key => $val)
				{
					$questions[] = $val;
				}
			}
		}
		
		return $questions;
	}

	public function get_question_attach($question_id, $size = 'square')
	{
		$attach = $this->fetch_all('question_attach', "question_id = " . intval($question_id), "is_image DESC");
		
		$attach_list = array();
		
		if ($attach)
		{
			foreach ($attach as $key => $data)
			{
				$attach_list[$data['id']] = array(
					'id' => $data['id'], 
					'is_image' => $data['is_image'], 
					'file_name' => $data['file_name'], 
					'access_key' => $data['access_key'], 
					'attachment' => get_setting('upload_url') . '/questions/' . date('Ymd', $data['add_time']) . '/' . $data['file_location'], 
					'thumb' => get_setting('upload_url') . '/questions/' . date('Ymd', $data['add_time']) . '/' . AWS_APP::config()->get('image')->attachment_thumbnail[$size]['w'] . 'x' . AWS_APP::config()->get('image')->attachment_thumbnail[$size]['h'] . '_' . $data['file_location']
				);
			}
		}
		
		return $attach_list;
	}

	
	/**
	 * 添加关键词
	 * @param  $question_id
	 * @param  $keyword
	 */
	public function add_question_keyword($question_id, $keyword='')
	{
		$question_id = intval($question_id);
		if((!$question_id)||(!$keyword))return false;
		
			$data = array(
					"question_id" => $question_id,
					"keyword" => trim($keyword),
					"add_time" => time()
			);
				
			$retval = $this->insert('question_keyword', $data);		
	}	
	
	/**
	 * 删除关键字
	 * @param  $question_id
	 */
	public function delete_question_keyword($question_id)
	{
		return $this->delete('question_keyword', "question_id = " . intval($question_id) );
	
	}
	
	/**
	 * 更新问题表字段

	 * @param $question_id
	 * @param $fields
	 */
	public function update_question_field($question_id, $fields)
	{
		$question_id = intval($question_id);
		
		if ($question_id <= 0)
		{
			return false;
		}
		
		return $this->update('question', $fields, "question_id = " . $question_id);
	}

	/**
	 * 更新问题回复计数
	 */
	public function update_answer_count($question_id)
	{
		$question_id = intval($question_id);
		
		if ($question_id <= 0)
		{
			return false;
		}
		
		$count = $this->count('answer', "question_id = " . $question_id);
		
		return $this->update_question_field($question_id, array(
			'answer_count' => $count
		));
	}

	/**
	 * 更新问题回复计数
	 */
	public function update_answer_users_count($question_id)
	{
		$question_id = intval($question_id);
		
		if ($question_id <= 0)
		{
			return false;
		}
		
		$count = $this->count('answer', "question_id = " . $question_id);
		
		return $this->update_question_field($question_id, array(
			'answer_users' => $count
		));
	}

	/**
	 * 更新问题关注计数
	 */
	public function update_focus_count($question_id)
	{
		$question_id = intval($question_id);
		
		if ($question_id <= 0)
		{
			return false;
		}
		
		$count = $this->count('question_focus', "question_id = " . $question_id);
		
		return $this->update_question_field($question_id, array(
			'focus_count' => $count
		));
	}

	/**
	 * 问题详细页面-相关问题
	 * @param $question_id
	 * @param $uid
	 */
	function get_relike_question_list($question_id,$uid=0,$limit=10)
	{
		$question=$this->get_question_info_by_id($question_id);
		
		if(!$question)
		{
			return false;
		}
		
		
		
		//根据话题ID,得到相关问题
		if ($question_topics = $this->model('question_topic')->get_question_topic_by_question_id($question_id)){
			foreach ($question_topics as $topic_info)
			{
				$topic_infos[] = $topic_info['topic_id'];
			}
		}
		
		$topic_question=array();
		
// 		if (! empty($topic_infos))
// 		{
// 			$q_links = $this->model('question_topic')->get_question_list_by_topic_id($topic_infos, $limit, "RAND()");
// 			if ($q_links)
// 			{
// 				foreach ($q_links as $key => $link)
// 				{
// 					if ($link['question_id'] == $question_id)
// 					{
// 						unset($q_links[$key]);
// 					}
// 					else
// 					{
// 						$question_lnk[$link['question_id']] = $link['question_content'];
// 					}
// 				}
// 			}
// 		}
		
		$question_info=array();
		if (! empty($topic_infos))
		{
			$rs = $this->model('question_topic')->get_question_list_by_topic_id($topic_infos, $limit, "RAND()");
			if ($rs)
			{
				foreach ($rs as $key => $row)
				{
					if ($row['question_id'] == $question_id)
					{
						continue;
					}
					else
					{
						$question_info[$row['question_id']]=$row;
						$topic_question[] = $row['question_id'];
					}
				}
			}
		}	
		
	
		if ($question_keywords=$this->get_question_keyword_list($question_id))
		{
			foreach($question_keywords as $qk_key=>$qk_val)
			{
				$keywords[]="'".$qk_val['keyword']."'";
				
			}
		}
		
		
		if($keywords)
		{
			if ($q_links2=$this->get_question_list_by_keywords($keywords))
			{
				foreach($q_links2 as $key=>$link)
				{
					
					if(in_array($link['question_id'], $topic_question))
					{
						$q_links2[$key]['question_topic']=1;
					}
					else
					{
						$q_links2[$key]['question_topic']=0;
					}
					
				}
				$q_links2 = aasort($q_links2, array(
					'-question_topic'
				));
			}
			
			
			//p($q_links2);
			
			
			if ($q_links2)
			{
				foreach($q_links2 as $key=>$link)
				{
					if ($link['question_id'] == $question_id)
					{
						unset($q_links2[$key]);
					}
					else
					{
						if(!isset($question_lnk[$link['question_id']]))
						{
						$question_lnk[$link['question_id']] = $link['question_content'];
						$question_info[$link['question_id']]=$link;
						}
					}
				}
			}
			
		}
		
		
		$q_links_num = count($question_lnk);
		
		/*if ($q_links_num < $limit)
		{
			
			$question_other=$this->get_user_publish($question['published_uid'],($limit-$q_links_num));
			
			//$same_answer_q = $this->get_question_same_answer($question_id, "", "0,100", "RAND()");
			
			foreach ($question_other as $same_question)
			{
				if ($same_question['question_id'] == $question_id)
				{
					continue;
				}
				
				if (! isset($question_lnk[$same_question['question_id']]))
				{
					$question_lnk[$same_question['question_id']] = $same_question['question_content'];
				}
			}
		}*/
		
		if($uid>0)
		{
			$questions_uninteresteds = $this->get_question_uninterested($uid);					
		}
			$questions_uninteresteds[] = 0;
			
		if (! $question_lnk)
		{
			return false;
		}
		
		
		foreach ($question_lnk as $key => $question_content)
		{
			if (in_array($key, $questions_uninteresteds))
				continue;
			
		
			$question_lnk_list[] = array(
				'question_id' => $key, 
				'question_content' => $question_content,
				'answer_count' => $question_info[$key]['answer_count']
			);
		}
		

		
		$question_lnk_list = array_slice($question_lnk_list, 0, 10);
		
		return $question_lnk_list;
	}

	/**
     * 格式化问题
     * 
     * @param  $question_list
     */
	function question_list_process($question_list)
	{
		if ($question_list)
		{
			foreach ($question_list as $qkey => $qval)
			{
				$user_ids[] = $qval["modify_uid"];
				$user_ids[] = $qval["published_uid"];
			}
		}
		if (is_array($user_ids))
		{
			$user_ids = array_unique($user_ids);
		}
		
		if ($user_info_arr = $this->model('account')->get_users_by_uids($user_ids))
		{
			foreach ($user_info_arr as $user)
			{
				$user_infos[$user['uid']] = $user;
			}
		}
		if (! is_array($question_list))
		{
			return false;
		}
		
		foreach ($question_list as $qkey => $qval)
		{
			//最后动作
			$question_list[$qkey]["last_action_str"] = $this->action_key_val[$qval["last_action"]];
			
			//最后动作真实用户名
			if ($question_list[$qkey]["last_action_str"])
			{
				$question_list[$qkey]["modify_real_name"] = $user_infos[$qval["modify_uid"]]['user_name'];
			}
			
			//是否已经关注
			if ($this->model('question')->has_focus_question($qval["question_id"], $this->user_id))
			{
				$question_list[$qkey]["has_focus_question"] = 1;
			}
			else
			{
				$question_list[$qkey]["has_focus_question"] = 0;
			}
			//得到回复
			$answer = $this->model('answer')->get_answer_list_by_question_id($qval["question_id"], 1);
			
			if ($answer)
			{
				$tmp = array();
				$tmp = FORMAT::format_content($answer[0]["answer_content"]);
				$question_list[$qkey]["answer_id"] = $answer[0]["answer_id"];
				$question_list[$qkey]["answer_title"] = $tmp['content_title'];
				$question_list[$qkey]["answer_content"] = $tmp['content_content'];
				//$question_list[$qkey]["answer"]=$answer[0]["answer_content"];
			}
			
			$question_list[$qkey]["avatar_file"] = $qval['avatar_file'];
			$question_list[$qkey]["published_username"] = $user_infos[$qval["published_uid"]]['user_name'];
		}
		
		return $question_list;
	}

	/**
	 *
	 * 得到用户感兴趣问题列表
	 * @param int $uid
	 * @return array
	 */
	public function get_question_uninterested($uid)
	{
		$data = array();
		
		if ($questions = $this->fetch_all('question_uninterested', 'uid = ' . intval($uid)))
		{
			foreach ($questions as $info)
			{
				$data[] = $info['question_id'];
			}
		}
		return $data;
	}

	/**
	 *
	 * 保存用户不感兴趣问题列表
	 * @param int $uid
	 * @param int $question_id
	 *
	 * @return boolean true|false
	 */
	public function add_question_uninterested($uid, $question_id)
	{
		$uid = intval($uid);
		$question_id = intval($question_id);
		
		if ($uid == 0 || $question_id == 0)
		{
			return false;
		}
		
		if (! $this->has_question_uninterested($uid, $question_id))
		{
			$data = array(
				"question_id" => $question_id, 
				"uid" => $uid, 
				"add_time" => time() //添加时间
			);
			return $this->insert('question_uninterested', $data);
		}
		else
		{
			return false;
		}
	
	}

	/**
	 *
	 * 删除用户不感兴趣问题列表
	 * @param int $uid
	 * @param int $question_id
	 *
	 * @return boolean true|false
	 */
	public function delete_question_uninterested($uid, $question_id)
	{
		return $this->delete('question_uninterested', "question_id = " . intval($question_id) . " AND uid = " . intval($uid));
	}

	public function has_question_uninterested($uid, $question_id)
	{
		$retval = $this->fetch_row('question_uninterested', "question_id = " . intval($question_id) . " AND uid = " . intval($uid));
		
		if ($retval)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public function add_invite($question_id, $sender_uid, $recipients_uid)
	{
		$question_id = intval($question_id);
		$sender_uid = intval($sender_uid);
		$recipients_uid = intval($recipients_uid);
		
		if ($question_id == 0 || $sender_uid == 0 || $recipients_uid == 0)
		{
			return false;
		}
		
		$data = array(
			'question_id' => $question_id, 
			'sender_uid' => $sender_uid, 
			'recipients_uid' => $recipients_uid, 
			'add_time' => time()
		);
		
		return $this->insert('question_invite', $data);
	}

	/**
	 * 发起者取消邀请
	 * @param unknown_type $question_id
	 * @param unknown_type $sender_uid
	 * @param unknown_type $recipients_uid
	 */
	public function cancel_question_invite($question_id, $sender_uid, $recipients_uid)
	{
		return $this->delete('question_invite', 'question_id = ' . intval($question_id) . ' AND sender_uid = ' . intval($sender_uid) . ' AND recipients_uid = ' . intval($recipients_uid));
	}

	/**
	 * 接收者删除邀请
	 * @param unknown_type $question_invite_id
	 * @param unknown_type $recipients_uid
	 */
	public function delete_question_invite($question_invite_id, $recipients_uid)
	{
		return $this->delete('question_invite', 'question_invite_id = ' . intval($question_invite_id) . ' AND recipients_uid = ' . intval($recipients_uid));
	}

	/**
	 * 接收者回复邀请的问题
	 * @param unknown_type $question_invite_id
	 * @param unknown_type $recipients_uid
	 */
	public function answer_question_invite($question_id, $recipients_uid)
	{
		return $this->delete('question_invite', 'question_id = ' . intval($question_id) . ' AND recipients_uid = ' . intval($recipients_uid));
	}

	public function check_question_invite($question_id, $sender_uid, $recipients_uid)
	{
		if($sender_uid == 0)
		{
			return $this->fetch_row('question_invite', 'question_id = ' . intval($question_id) . ' AND recipients_uid = ' . intval($recipients_uid));
		}
		else
		{
			return $this->fetch_row('question_invite', 'question_id = ' . intval($question_id) . ' AND sender_uid = ' . intval($sender_uid) . ' AND recipients_uid = ' . intval($recipients_uid));
		}
	}

	public function get_invite_users($question_id, $sender_uid)
	{
		$invites = $this->fetch_all('question_invite', 'question_id = ' . intval($question_id) . ' AND sender_uid = ' . intval($sender_uid), 'question_invite_id DESC');
		
		if (empty($invites))
		{
			return array();
		}
		
		$invite_users = array();
		
		foreach ($invites as $key => $val)
		{
			$invite_users[] = $val['recipients_uid'];
		}
		
		$invite_users = array_unique($invite_users);
		
		$user_info = $this->model('account')->get_users_by_uids($invite_users, true);
		
		if (empty($user_info))
		{
			return array();
		}
		
		foreach ($invites as $key => $val)
		{
			$user = $user_info[$val['recipients_uid']];
			$data[] = array(
				'uid' => $user['uid'], 
				'user_name' => $user['user_name'], 
				'signature' => $user['signature'], 
				'avatar_file' => $user['avatar_file'] ? get_setting('upload_url') . '/avatar/' . $user['avatar_file'] : get_setting('base_url') . '/static/common/avatar-mid-img.jpg', 
			);
		}
		
		return $data;
	}

	public function get_invite_question_list($uid, $limit = '10', $count = false)
	{
		$uid = intval($uid);
		
		if ($uid == 0)
		{
			return false;
		}

		$sql = "SELECT a.*, b.* FROM " . get_table('question') . " AS a LEFT JOIN " . get_table('question_invite') . " AS b ON a.question_id = b.question_id WHERE b.recipients_uid = " . $uid;
			
		$rs = $this->query_all($sql, $limit);
		
		if($count)
		{
			return count($rs);
		}
		else 
		{
			return $rs;
		}
	}
	
	public function parse_at_user($content, $popup = false, $with_user = false)
	{
		/*
		$content = '郑强，ss';
		
		if(preg_match('/(，|：)/is', $content))
		{
			$content = preg_replace_callback('/(，|：)/is', 'aaa', $content);
			$content = strstr($content, '，', true);
		}
		
		echo $content;die;
		
		preg_match_all('/@([^@,:\s,]+)/i', $content, $matchs);
		
		p($matchs);
		die;
		*/
		
		preg_match_all('/@([^@,:\s,]+)/i', $content, $matchs);
		
		if (is_array($matchs[1]))
		{
			$match_name = array();
				
			foreach ($matchs[1] as $key => $username)
			{
				if(in_array($username, $match_name))
				{
					continue;
				}
	
				$match_name[] = $username;
			}
				
			$match_name = array_unique($match_name);
			
			$all_users = array();
				
			foreach($match_name as $key => $username)
			{
				$user_info = $this->model('account')->get_users_by_username($username);
				
				if($user_info)
				{
					$content = str_replace('@' . $username, '<a href="people/' . $username . '"' . (($popup) ? ' target="_blank"' : '') . ' onmouseover="eventsMouseM(this);" rel="' . $user_info['uid'] . '">@' . $username . '</a>', $content);
					
					if($with_user)
					{
						$all_users[] = $user_info['uid'];
					}
				}
			}
		}
		
		if($with_user)
		{
			return array(
				'content' => $content,
				'user' => $all_users,
			);
		}
		else
		{
			return $content;
		}
	}
	
	public function update_question_comments_count($question_id)
	{
		$count = $this->count('question_comments', "question_id = " . intval($question_id));
		
		$this->update('question', array(
			'comment_count' => $count
		), "question_id = " . intval($question_id));
	}

	public function insert_question_comment($question_id, $uid, $message)
	{
		$data = array(
			'uid' => intval($uid), 
			'question_id' => intval($question_id), 
			'message' => htmlspecialchars($message), 
			'time' => time()
		);
		
		$comment_id = $this->insert('question_comments', $data);
		
		$this->update_question_comments_count($question_id);
		
		return $comment_id;
	}

	public function get_question_comments($question_id)
	{
		return $this->fetch_all('question_comments', "question_id = " . intval($question_id), "time ASC");
	}
	

	/**
	 * 处理话题日志
	 * @param array $log_list
	 *
	 * @return array
	 */
	public function analysis_log($log_list)
	{
		if (empty($log_list))
		{
			return $log_list;
		}
		
		//p($log_list);die;
		
		foreach ($log_list as $key => $log)
		{
			$title_list = "";
			$user_info = $this->model('account')->get_users_by_uid($log['uid']);
			$user_name = $user_info['user_name'];
				
			switch ($log['associate_action'])
			{
				case ACTION_LOG::ADD_QUESTION :
					$title_list = '<a href="people/' . $user_name . '">' . $user_name . '</a> 添加了该问题</p><p>' . $log['associate_content'] . '</p><p>' . $log['associate_attached'] . '';
					break;

				case ACTION_LOG::MOD_QUESTON_TITLE : //修改问题标题
					$Diff = new FineDiff($log['associate_attached'], $log['associate_content']);
						
					$rendered_diff = $Diff->renderDiffToHTML();
						
					$title_list = '<a href="people/' . $user_name . '">' . $user_name . '</a> 修改了问题标题<p>' .
	
							$rendered_diff . '</p>';
						
					break;

				case ACTION_LOG::MOD_QUESTION_DESCRI : //修改问题
					$Diff = new FineDiff($log['associate_attached'], $log['associate_content']);
						
					$rendered_diff = $Diff->renderDiffToHTML();
						
					$title_list = '<a href="people/' . $user_name . '">' . $user_name . '</a> 修改了问题补充内容<p>' .
	
							$rendered_diff . '</p>';
						
					break;

				case ACTION_LOG::ADD_TOPIC : //添加话题
					
					$title_list = '<a href="people/' . $user_name . '">' . $user_name . '</a> 给该问题添加了一个话题 <p><a href="topic/' . $log['associate_attached'] . '">' . $log['associate_content'] . '</a>';
						
					break;

				case ACTION_LOG::DELETE_TOPIC : //移除话题
					
					$title_list = '<a href="people/' . $user_name . '">' . $user_name . '</a> 移除了该问题的一个话题 <p><a href="topic/' . $log['associate_attached'] . '">' . $log['associate_content'] . '</a>';
						
					break;

				case ACTION_LOG::MOD_QUESTION_CATEGORY : //修改分类
					
					$title_list = '<a href="people/' . $user_name . '">' . $user_name . '</a> 修改了该问题的分类 <p><a href="home/explore/category-' . $log['associate_attached'] . '">' . $log['associate_content'] . '</a>';
						
					break;

				case ACTION_LOG::MOD_QUESTION_ATTACH : //修改附件
					
					$title_list = '<a href="people/' . $user_name . '">' . $user_name . '</a> 修改了该问题的附件 ';
						
					break;
			}
				
			(! empty($title_list)) ? $data_list[] = array(
					'title' => $title_list,
					'add_time' => date('Y-m-d H:i:s', $log['add_time']),
					'log_id' => sprintf('%06s', $log['history_id'])
			) : '';
		}
	
		return $data_list;
	}
	
	
	/**
	 * 分词处理
	 * @param  $quesiton_id 
	 * @param  $question_content
	 */
	function question_phpanalysis_keyword($question_id,$question_content)
	{
			$question_id=intval($question_id);
			
			$pa = new Services_Phpanalysis_Phpanalysis('utf-8', 'utf-8', false);
			//载入词典
			$pa->LoadDict();
			//执行分词
			
			$question_content_tmp = clear_point($question_content);			
			$pa->SetSource($question_content_tmp);			
			$pa->StartAnalysis(true);			
			$okresult = $pa->GetFinallyResult(',',false);			
			$okresult_array = preg_split("/,/", $okresult);
						
			if(is_array($okresult_array))
			{
				$this->delete_question_keyword($question_id);
					
				$okresult_array=array_unique($okresult_array);
				foreach ($okresult_array as $py_key=>$py_val)
				{
					$py_val=trim($py_val);
					{
						if($py_val)
						{
							if(cjk_strlen($py_val,'utf-8')>1)
							{
							$this->model('question')->add_question_keyword($question_id,$py_val);
							}
						}
					}
				}
					
			}
		
	}
}